﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Repository;
using Estimate.Common.Entity;

namespace Estimate.Reports
{
    /// <summary>
    /// 首页城市排名报表
    /// </summary>
   public  class HomeCityRankReport
   {
       #region 变量与对象
       /// <summary>
        /// 是否计算13个城市排名,默认计算(true)
       /// </summary>
       public bool CulculateCollectionRank = true;
       /// <summary>
       /// 是否计算省台数据,默认计算(true)
       /// </summary>
       public bool CulculateStSite = true;
       /// <summary>
       /// 是否计算预报偏差,默认计算(true)
       /// </summary>
       public bool CulculateDeviation = true;
       /// <summary>
       /// 是否计算个人排名,默认计算(true)
       /// </summary>
       public bool CulculateIndividualRank = true;

       /// <summary>
       /// 是否获取缺失排班表的站点,默认(true)
       /// </summary>
       public bool GetLackSchedules = true;

       CollectionRankReport collectionRankReport;//计算13个县市排名
       CollectionRankType type = CollectionRankType.area;

       CollectionRankReport stSiteReportScore;//计算省台

       FullInfoReport fullInfoReport;//计算预报偏差

       IndividualRankReport individualRankReport;//计算个人排名

       StationsOfLackScheduleReport stationsOfLackScheduleReport;//获取缺失排班表的站点

       string _connectionString = "";
       #endregion

       #region 构造函数
       public HomeCityRankReport(string connectionString)
       {
           _connectionString = connectionString;

           if (CulculateCollectionRank)
               collectionRankReport = new CollectionRankReport(connectionString, type);

           if (CulculateStSite)
           {
               Dictionary<string, string> cities = new Dictionary<string, string>() { { "省台", "00001" } };//省台预报数据是单独的表，不需要筛选，所有记录都要统计，
               stSiteReportScore = new CollectionRankReport(connectionString, ForeRepositoryFactory.StSiteForeRepository(connectionString), cities);
           }

           if (CulculateDeviation)
           {
               fullInfoReport = new FullInfoReport(connectionString, ForeRepositoryFactory.NormalCityForeRepository(_connectionString));
               fullInfoReport.CulculateDeviationResult = true;

           }

           if (CulculateIndividualRank)
               individualRankReport = new IndividualRankReport(connectionString);

           if (GetLackSchedules)
               stationsOfLackScheduleReport = new StationsOfLackScheduleReport(connectionString);
       }
       #endregion

       #region 数据准备并计算
       public void PrepareData(DateTime beginDate, DateTime endDate, ForeSemiType semitype)
       {
          
           ForeSpanType[] spans = new ForeSpanType[] { ForeSpanType.FD00_24, ForeSpanType.FD24_48, ForeSpanType.FD48_72, ForeSpanType.FD72_96, ForeSpanType.FD96_120 };
           ForeSendpointType[] sendpoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };

           if (CulculateCollectionRank)
               collectionRankReport.PrepareData(beginDate, endDate, semitype, spans, sendpoints);

           if (CulculateStSite)
               stSiteReportScore.PrepareData(beginDate, endDate, semitype, spans, sendpoints);

           if (CulculateDeviation)
               fullInfoReport.PrepareData(beginDate, endDate, semitype, spans, null, sendpoints);

           if (GetLackSchedules)
               stationsOfLackScheduleReport.PrepareData(beginDate, endDate);

           if (CulculateIndividualRank)
               individualRankReport.PrepareData(beginDate, endDate, semitype); 
       }
       #endregion

       #region 获取得分字符串,字符串为JSON格式
       /// <summary>
       /// 获取得分字符串,字符串为JSON格式
       /// </summary>
       /// <param name="previousScoreResult">(上个月或上上个月...的得分)与本次得分对比，得出名次和得分的趋势</param>
       /// <returns></returns>
       public string GetScoreResult(string previousScoreResult)
       {
           FinalRankScore finalRankScore = new FinalRankScore();
           List<ResultScore> result1 = null, result2 = null;

           if (CulculateCollectionRank)
           {
               if (previousScoreResult != null && previousScoreResult != "")
               {
                   FinalRankScore previous = Newtonsoft.Json.JsonConvert.DeserializeObject<FinalRankScore>(previousScoreResult);
                   result1 = collectionRankReport.GetScoreResult(previous.score_borad);
               }
               else
                   result1 = collectionRankReport.GetScoreResult(result1);//13城市成绩及排名

           }

           if (CulculateStSite)
           {
               result2 = stSiteReportScore.GetScoreResult(result2);//省台成绩
               for (int i = 0; i < result1.Count; i++)
                   result1[i].cities.Add(result2[i].cities[0].Copy());
           }

           finalRankScore.score_borad = result1;

           if (CulculateDeviation)
               finalRankScore.deviation = fullInfoReport.GetDeviationResult();//预报偏差

           if (CulculateIndividualRank)
               finalRankScore.top3_forecaster = individualRankReport.GetForecasterRank(3);//排名前3的个人

           if (GetLackSchedules)
              finalRankScore.lack_schedule= stationsOfLackScheduleReport.GetStationsOfLackSchedule();//获取缺失排班表的站点

           return Newtonsoft.Json.JsonConvert.SerializeObject(finalRankScore);
       }
       #endregion

   }

    #region 用于JSON序列化的类
    public class FinalRankScore
    {
        public List<ResultScore>  score_borad;
        public Deviation deviation;
        public List<string> lack_schedule;
        public List<Dictionary<string, object>> top3_forecaster;
    }
   #endregion
  
}
